using System.Data;
using MySqlConnector;
namespace Front._1118;

public partial class EditBookForm : Form
{
    public int bookId;
    public EditBookForm()
    {
        InitializeComponent();
    }


    private void EditBookForm_Load(object sender, EventArgs e)
    {
        //初始化加载
        Init();
    }

    private void Init()
    {
        //加载出版社信息
        GetAllPublishers();
        //加载分类信息
        //加载书架信息
        
        //实现所有的数据绑定
        //1.根据传递的id先查询此图书所有的数据
        //2.结合窗体的组件进行数据关联
        GetBookById(this.bookId);
    }

    private void GetBookById(int bookId)
    {
        // 实现登录逻辑
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "SELECT * FROM book_info where book_id = @bookId";
        using var command = new MySqlCommand(sql, connection);
        // 设置参数
        command.Parameters.AddWithValue("bookId", bookId);
        
        using var reader = command.ExecuteReader();
        while (reader.Read())
        {
            this.isbn.Text = reader.GetString(1);
            this.book_name.Text = reader.GetString(2);
            this.author.Text = reader.GetString(3);
            this.publisher.SelectedValue = reader.GetInt32(4);
        }
    }

    private void GetAllPublishers()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "SELECT publisher_id, publisher_name FROM publisher_info";
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //填充数据集
        DataSet dataSet = new DataSet();
        mySqlDataAdapter.Fill(dataSet, "publisher_info");

        if (dataSet.Tables["publisher_info"].Rows.Count > 0)
        {
            DataTable publisherTable = dataSet.Tables["publisher_info"];
            // 插入一行全部
            DataRow row = publisherTable.NewRow();
            row[0] = 0;
            row[1] = "全部";
            publisherTable.Rows.InsertAt(row, 0);

            publisher.DataSource = publisherTable;
            publisher.ValueMember = publisherTable.Columns[0].ColumnName;
            publisher.DisplayMember = publisherTable.Columns[1].ColumnName;
        }
    }


    
    // 保存方法
    private void button1_Click(object sender, EventArgs e)
    {
        // 获取组件的值
        string bookName = this.book_name.Text;
        string isbn = this.isbn.Text;
        //  获取所选出版社编号
        int publisherId = (int)this.publisher.SelectedValue;
        string author = this.author.Text;
        
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();

        string sql = "update book_info set isbn = @isbn, book_name = @bookName,author = @author, publisher_id = @publisherId \nwhere book_id = @bookId;";
        using var command = new MySqlCommand(sql, connection);
        // 设置参数
        command.Parameters.AddWithValue("isbn", isbn);
        command.Parameters.AddWithValue("bookName", bookName);
        command.Parameters.AddWithValue("author", author);
        command.Parameters.AddWithValue("publisherId", publisherId);
        command.Parameters.AddWithValue("bookId", this.bookId);
        int count = command.ExecuteNonQuery();
        if (count > 0)
        {
            // 更新成功
            MessageBox.Show("更新成功");
            this.DialogResult = DialogResult.OK;
            // 关闭窗体
            this.Close();
        }
        else
        {
            // 更新失败
            MessageBox.Show("更新失败");
            this.DialogResult = DialogResult.Cancel;
        }
    }
}